package com.ssyc.gsk_parents.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.utils.TextUtils;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BannerManager;
import com.ssyc.common.manager.PickChooseManager;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.dialog.CustomDialogManager;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.bean.EventDetailsInfo;
import com.ssyc.gsk_parents.bean.EventInfo;
import com.ssyc.gsk_parents.bean.ResultInfo;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 活动详情
 * Created by Administrator on 2018/7/13 0013.
 */

public class ParentsEventDetailsActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout rlBack;
    private Banner banner;
    private TextView tvNum;
    /**
     * 我是活动标题
     */
    private TextView tvName;
    /**
     * 新概念1班,新概念2班
     */
    private TextView tvTarget;
    /**
     * 2017-1-25 10:00~11:00
     */
    private TextView tvTime;
    /**
     * 国贸新都
     */
    private TextView tvAddress;
    /**
     * 国贸新都
     */
    private TextView tvContent;
    /**
     * 报名名单
     */
    private Button btLookPeople;
    private LinearLayout llConent;
    private RelativeLayout rlLoading;

    //页面传递的对象
    private EventInfo.ListBean info;

    //服务器返回的页面数据对象
    private EventDetailsInfo detailsInfo = null;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_event_details;
    }

    @Override
    protected void init() {
        showContent();

        //配置状态栏颜色
        initStateColor();

        //初始化视图
        initView();

        //获取Intent对象
        initIntent();

        //请求服务器获取数据
        http();
    }


    /**
     * 配置状态栏颜色的方法
     */
    private void initStateColor() {
        //设置6.0以后沉浸状体栏的问题
        StateBarManager.setBarWithUltimateBar(this, getResources().getColor(com.ssyc.common.R.color.titlebg),
                getResources().getColor(com.ssyc.common.R.color.nav_color));
    }


    /**
     * 初始化视图
     */
    public void initView() {
        rlBack = findViewById(R.id.rl_back);
        rlBack.setOnClickListener(this);
        banner = findViewById(R.id.banner);
        tvName = findViewById(R.id.tv_name);
        tvTarget = findViewById(R.id.tv_target);
        tvTime = findViewById(R.id.tv_time);
        tvAddress = findViewById(R.id.tv_address);
        tvContent = findViewById(R.id.tv_content);
        btLookPeople = findViewById(R.id.bt_look_people);
        btLookPeople.setOnClickListener(this);
        llConent = findViewById(R.id.ll_conent);
        tvNum = findViewById(R.id.tv_num);
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
        llConent.setVisibility(View.GONE);
    }

    /**
     * 获取Intent对象
     */
    private void initIntent() {
        info = (EventInfo.ListBean) getIntent().getSerializableExtra("data");


        //根据家长是否报名的状态来设置显示的内容
        if (TextUtils.isEmpty(info.getStatus2())) {
            //未报名
            if ("1".equals(info.getStatus())) {
                //进行中,不能报名
                btLookPeople.setText("进行中");
                btLookPeople.setEnabled(false);
            } else if ("0".equals(info.getStatus())) {
                //未开始,可以报名
                btLookPeople.setText("我要报名");
                btLookPeople.setEnabled(true);
            } else if ("2".equals(info.getStatus())) {
                //已结束,不能报名
                btLookPeople.setText("已结束");
                btLookPeople.setEnabled(false);
            }
        } else {
            //已报名
            btLookPeople.setText("已报名");
            btLookPeople.setEnabled(false);
        }

    }

    /**
     * 请求页面数据
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "5");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("rowid", info.getRowId() + "");
        map.put("apptoken", AccountUtils.getToken(this));
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.EVENTDETAILS, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                if (llConent != null && llConent.getVisibility() != View.VISIBLE) {
                    llConent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {

                    try {
                        detailsInfo = GsonUtil.jsonToBean(response, EventDetailsInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    Log.i("test", "活动：" + response);
                    if (null != detailsInfo) {
                        if ("200".equals(detailsInfo.getState())) {

                            //设置轮播图的数据
                            setBanner();

                            //设置页面其它的数据
                            setPageData();

                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + detailsInfo.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + detailsInfo.getState());
                        }
                    } else {
                        Log.i("test", Constants.EMPTY);
                    }
                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 设置轮播图的数据
     */
    private void setBanner() {
        if (detailsInfo != null) {
            List<String> bannerUrls = new ArrayList<>();
            if (!TextUtils.isEmpty(detailsInfo.getPicture1())) {
                bannerUrls.add(detailsInfo.getPicture1());
            }
            if (!TextUtils.isEmpty(detailsInfo.getPicture2())) {
                bannerUrls.add(detailsInfo.getPicture2());
            }

            if (!TextUtils.isEmpty(detailsInfo.getPicture3())) {
                bannerUrls.add(detailsInfo.getPicture3());
            }

            if (!TextUtils.isEmpty(detailsInfo.getPicture4())) {
                bannerUrls.add(detailsInfo.getPicture4());
            }
            if (!TextUtils.isEmpty(detailsInfo.getPicture5())) {
                bannerUrls.add(detailsInfo.getPicture5());
            }
            if (!TextUtils.isEmpty(detailsInfo.getPicture6())) {
                bannerUrls.add(detailsInfo.getPicture6());
            }
            if (!TextUtils.isEmpty(detailsInfo.getPicture7())) {
                bannerUrls.add(detailsInfo.getPicture7());
            }
            if (!TextUtils.isEmpty(detailsInfo.getPicture8())) {
                bannerUrls.add(detailsInfo.getPicture8());
            }
            if (!TextUtils.isEmpty(detailsInfo.getPicture9())) {
                bannerUrls.add(detailsInfo.getPicture9());
            }

            if (bannerUrls.size() == 0) {
                List<Integer> imgs = new ArrayList<>();
                imgs.add(R.drawable.base_event_def);
                //证明没有上传图片,那么现实默认的本地图片
                BannerManager.setDataLocal(banner, imgs, Transformer.Default);
            } else {
                BannerManager.setData(banner, true, bannerUrls, Transformer.Default);
                //设置点击事件
                banner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        PickChooseManager.startShowBigActivity(ParentsEventDetailsActivity.this, bannerUrls, position);
                    }
                });
            }
        }
    }

    /**
     * 设置页面其它显示数据
     */
    private void setPageData() {
        if (detailsInfo != null) {

            //轮播图的标题
            tvName.setText(detailsInfo.getTag());

            //报名人数
            if (TextUtils.isEmpty(detailsInfo.getNum())) {
                tvNum.setText("0");
            } else {
                tvNum.setText(detailsInfo.getNum());
            }

            //活动对象
            tvTarget.setText(info.getTarget());

            //活动时间
            tvTime.setText(detailsInfo.getBegintime() + "~" + detailsInfo.getEndtime());

            //活动地点
            tvAddress.setText(detailsInfo.getAddress());

            //活动简介
            tvContent.setText(detailsInfo.getContent());
        }
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_back) {
            //返回
            finish();
        } else if (id == R.id.bt_look_people) {
            //我要报名,弹框提示是否需要报名
            showDialog();
        }
    }

    /**
     * 弹框
     */
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("确定进行报名吗?");//提示内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                doApplyAction();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /**
     * 报名的操作
     */
    private void doApplyAction() {
        CustomDialogManager.show(this, "报名中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "4");
        map.put("acc", AccountUtils.getAccount(this));
//        map.put("acc", "18827399806");
        map.put("role", AccountUtils.getRole(this));
        map.put("rowid2", info.getRowId() + "");
        map.put("apptoken", AccountUtils.getToken(this));
        map.put("platform", "8");

        Log.i("test", "rowid2:" + info.getRowId() + "apptoken:" + AccountUtils.getToken(this));
        HttpUtils.post(HttpAdress.PARENTEVENTBAOMING, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast("报名失败" + BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo resinfo = null;
                    try {
                        resinfo = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != resinfo) {
                        if ("200".equals(resinfo.state)) {
                            UiUtils.Toast("报名成功", false);

                            BusInfo busInfo = new BusInfo();
                            busInfo.setType(ParentEventActivity.UPDATE);
                            EventBus.getDefault().post(busInfo);

                            // 关闭当前页面
                            finish();
                        } else {
                            UiUtils.Toast("报名失败" + Constants.ERRORSTATE + resinfo.state, false);
                            Log.i("test", Constants.ERRORSTATE + resinfo.state);
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });

    }
}
